using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ACM.cmsarea08;
using ACM.cmsarea08.Controls;
using ACM.cmsarea08.Administration;


namespace ACM.cmsarea08.Administration.Core {
	public partial class GesBloccoVisivo :  BaseDGridPage {
		private const int offSet = 20;
		protected Itan_DataGrid insVBlock;
		private ACM.cmsarea08.Administration.Core.DBClass dclass = new cmsarea08.Administration.Core.DBClass();
		
		private int TotalePagine {
			get {
				if(ViewState["pagine"]==null)
					return -1;
				else
					return (int)ViewState["pagine"];
			}
			set {
				ViewState["pagine"]=value;
			}
		}
		private int PaginaCorrente {
			get {
				if(ViewState["PaginaCorrente"]==null)
					return -1;
				else
					return (int)ViewState["PaginaCorrente"];
			}
			set {
				ViewState["PaginaCorrente"]=value;
			}
		}
		
		
		override protected void OnInit(EventArgs e)
		{
			if(ACM.cmsarea08.Users.CurrentUser.Role.Score>400)
				Response.Redirect("/loginadm.aspx?returnurl="+
								HttpUtility.UrlEncode(Request.ServerVariables["SCRIPT_NAME"].ToString()+
				                                        (Request.ServerVariables["QUERY_STRING"].ToString()==""?"":"?"+Request.ServerVariables["QUERY_STRING"].ToString()))
	);
			else if(ACM.cmsarea08.Users.CurrentUser.Role.Score>100)
				Response.Redirect("/administration/accessdeny.aspx");
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
			if(this.Master is ACM.cmsarea08.Administration.Core.ListerPrivate) {
//				((itareanet.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.EditMode=TipiPagineLista.BlocchiVisivi;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowAddDefault=false;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DBActionTable="blocco_visivo";
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.InsertCommand+= new EventHandler(Inserisci_BloccoV);
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.HelpPath="/administration/docs/BlocchiV.html";
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DataSourceChanged+= new EventHandler(RicaricaDati);
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DeletingQueryCheck="select(select count(*) from " + ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + "moduli where blocco_visivo='{0}')+" +
					"(select if(lockid=-1,0,1) from " + ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle + "blocco_visivo where id='{0}');";
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.DeletingQueryCheckFailMessage="Impossibile eliminare. Scollegare prima tutti i moduli";
			}
			
			this.Load += new System.EventHandler(this.Page_Load);
			insVBlock.CambiaPagina += new Itan_DataGridEventHandler(CambiaPagina);
			insVBlock.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(GrigliaCommand);
			insVBlock.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(MentreCaricaDati);
				
		}
	
		private void Page_Load(object o , EventArgs e) {
			CaricaDati();
			insVBlock.Title = rm.GetString("vblock_list");
			insVBlock.OrderText = rm.GetString("grid_ordine");
			if(!Page.IsPostBack) {
				
			}
		}
		private void CaricaDati() {
			if(PaginaCorrente==-1)
				PaginaCorrente = 0;
			
			if(ElencoGriglia==null) {
				insVBlock.DataSource=null;
				ElencoGriglia = dclass.GetAdminBlocchiVisivi((PaginaCorrente*offSet),offSet);
			}
			if(ElencoGriglia==null) {
				insVBlock.Visible=false;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowClone=false;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowDelete=false;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ListButtonBind();
				return;
			}
			if(ElencoGriglia.Tables[0].Columns.Contains("uconteggio") && 
			   ElencoGriglia.Tables[0].Rows.Count>0 ) {
				int totaleC =0;
				Int32.TryParse(ElencoGriglia.Tables[0].Rows[0]["uconteggio"].ToString(), out totaleC);
				TotalePagine = (totaleC/offSet) + (totaleC%offSet>1?1:0);
				
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowClone=
					ACM.cmsarea08.Users.CurrentUser.Role.Layout.CanManageViewBlocks && true;
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ShowDelete=
					ACM.cmsarea08.Users.CurrentUser.Role.Layout.CanManageViewBlocks && true;
				
				((ACM.cmsarea08.Administration.Core.ListerPrivate)this.Master).adm_button.ListButtonBind();
			}


			insVBlock.PageNum=TotalePagine;
			insVBlock.CurrentPage = PaginaCorrente;
			insVBlock.Visible=true;
			if(insVBlock.DataSource==null) {
				insVBlock.DataSource = ElencoGriglia;
				insVBlock.DataBind();
			}
			
		}
		private void Inserisci_BloccoV(object o, EventArgs e){
			
			Response.Redirect("edit_bloccov.aspx?new=1&idf=0");
			
		}
		
		private void CambiaPagina(object o, Itan_DataGridEventArgs e) {
			PaginaCorrente=e.NewPage;
			ElencoGriglia=null;
			CaricaDati();
		}
		
		private void RicaricaDati(object o, EventArgs e) {
			ElencoGriglia=null;
			CaricaDati();
		}
		private void MentreCaricaDati(object o, DataGridItemEventArgs e) {
			//enable_me
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
				LinkButton lkb;
				lkb = (LinkButton)e.Item.FindControl("enable_me");
				if(lkb!=null) {
					string ena = ((DataRowView)e.Item.DataItem)["enabled"].ToString();
					lkb.CommandName="Abilita";
					lkb.CommandArgument=((DataRowView)e.Item.DataItem)["id"].ToString();
					lkb.Text = 
						(ena=="1"? ("<img border=\"0\" class=\"jmsenabled1_sm\" src=\"../template/img/spacer.gif\" alt=\""+ rm.GetString("enabled") +"\" />"):
						 ("<img border=\"0\" class=\"jmsenabled0_sm\" src=\"../template/img/spacer.gif\" alt=\""+ rm.GetString("disabled") +"\" />"))
						;
				}
				if(ElencoGriglia.Tables[0].Columns.Contains("lockid") && ((DataRowView)e.Item.DataItem)["lockid"].ToString().Trim()!="-1")
					e.Item.ForeColor = System.Drawing.Color.Red;
			}
			
		}
		private void GrigliaCommand(object o, DataGridCommandEventArgs e) {
			//enable_me
			string id="";
			if(e.CommandName.ToString() == "Abilita") {
				id=e.CommandArgument.ToString();
				int reso = dclass.ExecuteScalar("update "+ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle+"blocco_visivo " +
				"set " +
				" enabled = if(enabled='1','0','1') " +				
				"where id = '" +id +"' limit 1;");
				if(reso>0) {
					ElencoGriglia=null;
					CaricaDati();
				} else {
					
				}
			}
		}
	}
	
}
